package com.xiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.domain.ResponseResult;
import com.xiao.domain.constants.BlogLinkConstants;
import com.xiao.domain.dto.AddLinkDto;
import com.xiao.domain.dto.ChangeLinkStatusDto;
import com.xiao.domain.dto.LinkDto;
import com.xiao.domain.dto.SaveUpdateLinkDto;
import com.xiao.domain.vo.PageVo;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.mapper.LinkMapper;
import com.xiao.domain.pojo.BlogLink;
import com.xiao.service.BlogLinkService;
import com.xiao.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class BlogLinkServiceImpl extends ServiceImpl<LinkMapper, BlogLink> implements BlogLinkService {
    @Override
    public ResponseResult getAllLink() {
        // 1. 构建查询条件
        LambdaQueryWrapper<BlogLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogLink::getStatus, BlogLinkConstants.LINK_APPROVED);// 只要审核通过的 友链。
        // 2. 查询
        List<BlogLink> list = this.list(wrapper);
        // 3. 转 VO
        List<BlogLink> blogLinks = BeanCopyUtils.copyBeanList(list, BlogLink.class);
        // 4. 包装响应
        ResponseResult okResult = ResponseResult.okResult(blogLinks);

        return okResult;
    }

    @Override
    public ResponseResult getLinkList(Integer pageNum, Integer pageSize, LinkDto linkDto) {
        // 1. 构建查询条件
        LambdaQueryWrapper<BlogLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(linkDto.getName()),BlogLink::getName,linkDto.getName());
        wrapper.eq(StringUtils.hasText(linkDto.getStatus()),BlogLink::getStatus,linkDto.getStatus());
        // 2. 分页查询
        Page<BlogLink> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        
        Page<BlogLink> page1 = this.page(page, wrapper);
        List<BlogLink> records = page1.getRecords();
        // 3. 统一pageVo
        PageVo pageVo = new PageVo(records, page1.getTotal());
        // 4. 统一响应

        ResponseResult responseResult = ResponseResult.okResult(pageVo);

        return responseResult;
    }

    @Override
    public ResponseResult AddLink(AddLinkDto addLinkDto) {
        // 1. 插入友链
        // 转 pojo
        BlogLink blogLink = BeanCopyUtils.copyBean(addLinkDto, BlogLink.class);
        boolean save = this.save(blogLink);
        // 2. 统一响应

        if (save){ // 保存成功
            return ResponseResult.okResult();
        }else {// 否则失败
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

    }

    @Override
    public ResponseResult getLinkById(Long id) {
        BlogLink blogLink = this.getBaseMapper().selectById(id);
        ResponseResult responseResult = ResponseResult.okResult(blogLink);

        return responseResult;
    }

    @Override
    public ResponseResult saveUpdateLink(SaveUpdateLinkDto saveUpdateLinkDto) {
        // 1. 提交修改
        BlogLink blogLink = BeanCopyUtils.copyBean(saveUpdateLinkDto, BlogLink.class);

        boolean isUpdate = this.updateById(blogLink);
        // 2. 统一响应
        ResponseResult responseResult;
        if (isUpdate){
            responseResult=ResponseResult.okResult();
        }else {
            responseResult=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult delLinkByIdList(List<Long> idList) {

        int i = this.getBaseMapper().deleteBatchIds(idList);

        ResponseResult responseResult;

        if (i>0){
            responseResult = ResponseResult.okResult();
        }else{
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult changeLinkStatus(ChangeLinkStatusDto changeLinkStatusDto) {
        // 1. 修改友链
        BlogLink blogLink = BeanCopyUtils.copyBean(changeLinkStatusDto, BlogLink.class);

        boolean isUpdate = this.updateById(blogLink);

        ResponseResult responseResult;

        if (isUpdate){
            responseResult = ResponseResult.okResult();
        }else{
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }
}
